package com.dgut.epidemic.controller;

import com.dgut.epidemic.mapper.UserMangerMapper;
import com.dgut.epidemic.pojo.Daily_data;
import com.dgut.epidemic.pojo.WeChatUser;
import com.dgut.epidemic.service.UserMangerService;
import com.dgut.epidemic.service.UserService;
import com.dgut.epidemic.service.WeChatUserService;
import com.dgut.epidemic.utils.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Author: wuhaoduan
 * Date: 2021/6/12 20:25
 */
@Controller
public class UserMangerController {

    @Autowired
    private UserMangerService userMangerService;
    @Autowired
    private UserService userService;
    @Autowired
    private WeChatUserService weChatUserService;

    int pageSize = 5;
    ExcelUtil excelUtil = new ExcelUtil();
    List<Daily_data> daily_data = new ArrayList<>();

        @GetMapping("/userManger/toUserMessage")
    public String toUserMessage(Model model,@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "staus", defaultValue = "0")int staus,
                                @RequestParam(value = "id", defaultValue = "0")int id,
                                @RequestParam(value = "type", defaultValue = "0")int type,
                                @RequestParam(value = "isSubmit", defaultValue = "0")int isSubmit
                                ) throws Exception {
        List<WeChatUser> weChatUsers;
        if(staus == 1){ weChatUsers=userMangerService.getWeChatUserOrderStaus(); }
        else if(type == 1){ weChatUsers=userMangerService.getWeChatUserOrderType(); }
        else if(id == 1){ weChatUsers=userMangerService.getWeChatUserOrderID(); }
        else if(isSubmit == 1){ weChatUsers=userMangerService.getWeChatUserOrderToday(); }
        else{ weChatUsers = userMangerService.getAllWeChatUser(); }
        List<WeChatUser> weChatUserList = userMangerService.getWeChatUserAfter(weChatUsers,pageNum,pageSize);
        model.addAttribute("weChatUsers",weChatUserList);
        model.addAttribute("roles",userService.getUserNow().getRoles());
        model.addAttribute("staus", staus);
        model.addAttribute("id", id);
        model.addAttribute("type", type);
        model.addAttribute("isSubmit", isSubmit);
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("lastPage", (weChatUsers.size()/pageSize)+1);
        return "userManger_message";
    }

    @RequestMapping("/userManger/lock")
    public String lock(Model model, Integer pageNum,String id,int staus, int sno, int type,int isSubmit)
            throws Exception {
        userMangerService.lockWeChatUser(id);
        return toUserMessage(model,pageNum,staus,sno,type,isSubmit);
    }

    @RequestMapping("/userManger/unlock")
    public String unlock(Model model,Integer pageNum,String id, int staus, int sno, int type,int isSubmit)
            throws Exception {
        userMangerService.unlockWeChatUser(id);
        return toUserMessage(model,pageNum,staus,sno,type,isSubmit);
    }

    @GetMapping("/userManger/toUserDailyData")
    public String toUserDailyData(Model model,Integer beforePageNum, Integer pageNum,String id,int staus, int sno, int type,int isSubmit)
            throws Exception {
        daily_data = userMangerService.getALLDailyDataById(id);
        System.out.println(daily_data);
        List<Daily_data> daily_data1 = userMangerService.getDailyDataAfter(daily_data,pageNum,pageSize);
        model.addAttribute("daily_data", daily_data1);
        model.addAttribute("staus", staus);
        model.addAttribute("Id", id);
        model.addAttribute("id", sno);
        model.addAttribute("type", type);
        model.addAttribute("isSubmit", isSubmit);
        model.addAttribute("beforePageNum",beforePageNum);
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("lastPage", (daily_data.size()/pageSize)+1);
        model.addAttribute("name", weChatUserService.getWeChatUserById(id).getName());
        return "userManger_showDaily";
    }

    @RequestMapping("/userManger/getDailyData")
    public void riskBodyExcel(HttpServletResponse response) throws IOException {
        System.out.println(daily_data);
        List<String> head = Arrays.asList("ID", "提交日期", "学号/工号", "手机号", "联系人", "联系人手机号", "身体状态", "是否就医", "是否隔离", "是否在校", "是否在莞", "目前所在地", "今日体温");
        excelUtil.writeExcel(response,daily_data,head);
    }

}
